
#import "TorrentDownloadItem.h"

@implementation TorrentDownloadItem

//
// name
//
@synthesize torrentName														= _torrentName;

//
// info hash
//
@synthesize infoHash														= _infoHash;

//
// state
//
@synthesize state															= _state;

//
// size
//
@synthesize size															= _size;

//
// downloaded size
//
@synthesize downloadedSize													= _downloadedSize;

//
// uploaded size
//
@synthesize uploadedSize													= _uploadedSize;

//
// download rate
//
@synthesize downloadRate													= _downloadRate;

//
// upload rate
//
@synthesize uploadRate														= _uploadRate;

//
// seeder count
//
@synthesize seederCount														= _seederCount;

//
// peer count
//
@synthesize peerCount														= _peerCount;

//
// total seeder count
//
@synthesize totalSeederCount												= _totalSeederCount;

//
// total peer count
//
@synthesize totalPeerCount													= _totalPeerCount;

//
// share ratio
//
@synthesize shareRatio														= _shareRatio;

//
// message
//
@synthesize message															= _message;

//
// base path
//
@synthesize basePath														= _basePath;

//
// left time
//
@synthesize leftTime														= _leftTime;

//
// dealloc
//
- (void)dealloc
{
	self.torrentName														= nil;
	self.infoHash															= nil;
	self.message															= nil;
	self.basePath															= nil;
	
	[super dealloc];
}

//
// get status
//
- (NSString*)status
{
	NSString* retValue														= nil;
	BOOL finished															= self.downloadedSize >= self.size;
	if(self.state & kTorrentStateChecking)
	{
		retValue															= NSLocalizedStringFromTable(@"Checking", @"TorrentDownloadItem", nil);
	}
	else if(self.state & kTorrentStateHashing)
	{
		retValue															= NSLocalizedStringFromTable(@"Queued", @"TorrentDownloadItem", nil);
	}
	else if(self.state & kTorrentStateStarted)
	{
		if(self.state & kTorrentStatePaused)
			retValue														= NSLocalizedStringFromTable(@"Pausing", @"TorrentDownloadItem", nil);
		else if(finished)
			retValue														= (self.state & kTorrentStateError) ? NSLocalizedStringFromTable(@"Seeding Error", @"TorrentDownloadItem", nil) : NSLocalizedStringFromTable(@"Seeding", @"TorrentDownloadItem", nil);
		else
			retValue														= (self.state & kTorrentStateError) ? NSLocalizedStringFromTable(@"Leeching Error", @"TorrentDownloadItem", nil) : NSLocalizedStringFromTable(@"Leeching", @"TorrentDownloadItem", nil);
	}
	
	if(!retValue && (self.state & kTorrentStateError))
		retValue															= NSLocalizedStringFromTable(@"Error", @"TorrentDownloadItem", nil);
	
	if(!retValue && (self.state & kTorrentStateQueued))
		retValue															= NSLocalizedStringFromTable(@"Queued", @"TorrentDownloadItem", nil);
	
	if(!retValue && finished)
		retValue															= NSLocalizedStringFromTable(@"Completed", @"TorrentDownloadItem", nil);

	if(!retValue && !finished)
		retValue															= NSLocalizedStringFromTable(@"Stopped", @"TorrentDownloadItem", nil);
	
	return retValue;
}

//
// can start
//
- (BOOL)canStart
{
	return (!(self.state & kTorrentStateStarted) || (self.state & kTorrentStatePaused)) && !(self.state & kTorrentStateChecking) && !(self.state & kTorrentStateHashing);
}

//
// can pause
//
- (BOOL)canPause
{
	return (self.state & kTorrentStateStarted) && !(self.state & kTorrentStatePaused) && !(self.state & kTorrentStateChecking) && !(self.state & kTorrentStateHashing);
}

//
// can stop
//
- (BOOL)canStop
{
	return (self.state & kTorrentStateStarted) || (self.state & kTorrentStateChecking) || (self.state & kTorrentStateHashing);
}

//
// can recheck
//
- (BOOL)canRecheck
{
	return !(self.state & kTorrentStateChecking) && !(self.state & kTorrentStateHashing);
}

@end
